
require 'gadgets/combobox'
module EventEditor
  module Commands
    class Message
      def self.show(base, model, edit_command = [], options = {}, &func)
       @dialog = Gtk::Dialog.new("Message",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @model = model
        @dialog.vbox.add(self.create_panel)
        @dialog.vbox.show_all

        self.load(edit_command)
        
        @dialog.run do |response|
          if response == Gtk::Dialog::RESPONSE_OK
            func.call(self.command)
          end
        end
        @dialog.destroy
      end
      
      def self.create_panel
        ## Create document
        box = Gtk::VBox.new
        
        @message_type_combobox = ::Gadgets::Combobox.new([["Normal Baloon", "normal_baloon"], ["Think Baloon", "think_baloon"], ["Shout Baloon", "shout_baloon"]], [String, String])
        box.add(@message_type_combobox)
        
        # Speaker Selection
        @event_combobox = ::Gadgets::Combobox.new(@model.event_select_list, [String, Integer])
        box.add(@event_combobox)

        # Text Area
        sw = Gtk::ScrolledWindow.new
        sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
        sw.shadow_type = Gtk::SHADOW_IN
        @text_area = Gtk::TextView.new
        sw.add(@text_area)
        box.add(sw)

        hbox = Gtk::HBox.new
        hbox.add(Gtk::Label.new("Close Time(-1 for key)"))
        hbox.add(@auto_close_time = Gtk::SpinButton.new(-1, 9999, 1))
        
        box.add(hbox)
        return box
      end
      
      def self.load(edit_command)
        if edit_command.empty?
          
        else
          @event_combobox.select(edit_command[2])
          @text_area.buffer.text = edit_command[1]
          @message_type_combobox.select(edit_command[3])
          @auto_close_time.value = edit_command[4] if edit_command[4]
        end
      end
      
      def self.command
        return ['message', @text_area.buffer.text, @event_combobox.selected_data, @message_type_combobox.selected_data, @auto_close_time.value.floor]
      end
      
      def self.command_to_s(model, args, margin = "")
        
        name = '[<span color="red">invalid</span>]'
        model.event_select_list.each do |t|
          if t[1] == args[2]
            name = "[ " + t[0] + " ]"
            break
          end
        end
        
        s = ""
        s +='<b>Message:</b> ' + name

        s += " :#{args[3]}"
        if args[3] == -1

        else
          s += " : #{args[4]}"
        end
        
        s += "\n"
        
        args[1].each_line do |line|
          s +=  margin + " <b>:                :</b>" + line
        end
        #s += '</span>'

        return s
      end
      
      def self.icon
        return @@icon
      end
    end
  end
end